package com.kuizii.generator.util.generator;

import com.kuizii.generator.util.RRException;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

/**
 * <p>
 * 获取Generator配置信息
 * </p>
 *
 * @author nuanyang
 * Email
 * created by 2018/6/20
 */
public class GeneratorConfig {

    /**
     * core名
     */
    static final String MODULE_CORE = "core";
    /**
     * biz名
     */
    static final String MODULE_BIZ = "biz";
    /**
     * api名
     */
    static final String MODULE_API = "api";
    /**
     * starter名
     */
    static final String MODULE_STARTER = "starter";
    /**
     * webapi名
     */
    static final String MODULE_WEB_API = "webapi";

    /**
     * 项目的前缀
     */

    /**
     * 项目中间
     *
     * @return
     */
    public static String getModulePrefix() {
        return getConfig().getString("moduleName") + "-";
    }

    public static String getPlatformType() {
        return getConfig().getString("platformType");
    }

    /**
     * 项目前缀
     *
     * @return
     */
    public static String getModuleParent() {
        return "kuizii-" ;
    }

    /**
     * 获取spring boot 目录
     *
     * @param func
     * @return
     */
    public static String getSpringBootRoute(String func) {
        return getProjectJavaRoute(func);
    }

    /**
     * 获取utils目录
     *
     * @param func
     * @return
     */
    public static String getUtilsRoute(String func) {
        return getProjectJavaRoute(func) + "utils" + File.separator;
    }


    /**
     * 获取pom目录
     *
     * @param func
     * @return
     */
    public static String getPomRoute(String func) {
        return getProjectName(func);
    }

    /**
     * 获取 fallback 目录
     *
     * @param func
     * @return
     */
    public static String getFallBackRoute(String func) {
        return getProjectJavaRoute(func) + "fallback" + File.separator;
    }

    /**
     * 获取controller目录
     *
     * @param func
     * @return
     */
    public static String getController(String func) {
        return getProjectJavaRoute(func) + "controller" + File.separator;
    }

    /**
     * 获取service 对外服务
     *
     * @return
     */
    public static String getService(String func) {
        return getProjectJavaRoute(func) + "service" + File.separator;

    }

    /**
     * 获取 domain base目录
     *
     * @return
     */
    public static String getDomainBase(String func) {
        return getProjectJavaRoute(func) + "domain" + File.separator + "base" + File.separator;
    }

    /**
     * 获取 domain dto 目录
     *
     * @return
     */
    public static String getDomainDto(String func) {
        return getProjectJavaRoute(func) + "domain" + File.separator + "dto" + File.separator;
    }

    /**
     * 获取 domain entity目录
     *
     * @return
     */
    public static String getDomainEntity(String func) {
        return getProjectJavaRoute(func) + "domain" + File.separator + "entity" + File.separator;
    }

    /**
     * 获取 data service impl 路径
     *
     * @return
     */
    public static String getDataSeiviceImplRoute(String func) {
        return getProjectJavaRoute(func) + "data" + File.separator + "service" + File.separator + "impl" + File.separator;
    }

    /**
     * 获取 data service 路径
     *
     * @return
     */
    public static String getDataServiceRoute(String func) {
        return getProjectJavaRoute(func) + "data" + File.separator + "service" + File.separator;
    }

    /**
     * 获取dao 4 java目录
     *
     * @return
     */
    public static String getMapperRoute(String func) {
        return getProjectJavaRoute(func) + "mapper" + File.separator;
    }

    /**
     * 获取mapper目录
     *
     * @return
     */
    public static String getMapperXMLRoute(String func) {
        return getProjectResourcesRoute(func) + "mapper" + File.separator + cutPackageName(getModuleName().replace("-", "."));
    }

    /**
     * 获取java目录
     *
     * @return
     */
    public static String getProjectJavaRoute(String func) {
        return getProjectPackage(func) + "java" + File.separator +
                (StringUtils.isBlank(getConfig().getString("package")) ? "com" + File.separator + "kuizii" + File.separator : cutPackageName(getConfig().getString("package")))
                + cutPackageName(getModuleName().replace("-", ".")) + func + File.separator;
    }


    /**
     * 根据包名.生文件目录
     *
     * @param name
     * @return
     */
    public static String cutPackageName(String name) {
        String[] packageSplit = name.split("\\.");
        StringBuilder packages = null;
        for (String spli : packageSplit) {
            if (packages == null) {
                packages = new StringBuilder(spli + File.separator);
            } else {
                packages.append(spli).append(File.separator);
            }
        }
        return packages.toString();

    }

    /**
     * 获取资源文件目录
     *
     * @return
     */
    public static String getProjectResourcesRoute(String func) {
        return getProjectPackage(func) + "resources" + File.separator;
    }

    /**
     * 获取项目
     *
     * @return
     */
    public static String getProjectPackage(String func) {
        return getProjectName(func) + "src" + File.separator + "main" + File.separator;
    }

    public static String getTestReasouseRoute(String func) {
        return getProjectName(func) + "src" + File.separator + "test" + File.separator + "resources" + File.separator;
    }

    /**
     * 获取SpringBoot Test目录
     *
     * @param func
     * @return
     */
    public static String getTestApplicationRoute(String func) {
        return getProjectName(func) + "src" + File.separator + "test" + File.separator + "java" + File.separator +
                (StringUtils.isBlank(getConfig().getString("package")) ? cutPackageName("com.kuizii") : cutPackageName(getConfig().getString("package")))
                + cutPackageName(getModuleName().replace("-", ".")) + func + File.separator;
    }

    /**
     * 获取项目名称
     *
     * @param func
     * @return
     */
    public static String getProjectName(String func) {
        return getProjectParentName() +
                getModuleParent() + getModuleName() + "-" + func + File.separator;
    }

    /**
     * 包名
     *
     * @param func
     * @return
     */
    public static String getProjectNameNotSlash(String func) {
        return getModuleParent() +
                (StringUtils.isBlank(getModuleName()) ? "demo" : getModuleName())
                + "-" +
                func;
    }


    /**
     * 项目名称
     *
     * @return
     */
    public static String getProjectName() {
        return getModuleParent() +
                (StringUtils.isBlank(getModuleName()) ? "demo" : getModuleName());
    }


    /**
     * 获取Generator 配置文件
     *
     * @return
     */
    public static Configuration getConfig() {
        try {
            PropertiesConfiguration configuration = new PropertiesConfiguration();
            configuration.load(new InputStreamReader(GeneratorConfig.class.getClassLoader().getResourceAsStream("generator.properties"), StandardCharsets.UTF_8));
            return configuration;
        } catch (ConfigurationException e) {
            e.printStackTrace();
            throw new RRException("配置文件获取失败,请检查---> generator.properties 文件是否存在", e);
        }
    }

    public static String getProjectParentName() {
        return getModuleParent() + GeneratorConfig.getModuleName() + File.separator;
    }

    /**
     * 获取module name
     *
     * @return
     */
    public static String getModuleName() {
        return getConfig().getString("moduleName");
    }
    /**
     * 获取module name
     *
     * @return
     */
    public static String getApiModuleName() {
        return getConfig().getString("apiModuleName");
    }

    public static String getEndPoint(){
        return getConfig().getString("endPoint");
    }
}
